<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
       $notes=M('notes');
       $lists=$notes->select();       //$lists是二维数组
       $this->assign("rows",$lists); 
       $this->display();
    }
    
    public function del()   //删除愿望（同步刷新）
    {       
       $id=I('get.id',0);
	
       $model=M('notes'); 	     
	$rs=$model->delete($id);
	if ($rs)
	   { 
          $this->success('删除成功', U('Home/Index/index'));  
	   }
	   else
	   { 
         $this->error('删除失败');
	   }
          
    }
    
    
    public function add()   //发表愿望(同步刷新)
    {
       $model=D("notes");
       if($model->create()) 
       {
           $model->addtime=date('Y-m-d H:i:s',time());
           $result=$model->add();
           if($result){
               $this->success('许愿成功', U('Home/Index/index')); 
            }
           else
            {
		$this->error('许愿失败');
	    }
         }
       else //数据通不过验证
           {
            $this->error($model->getError());//输出错误提示信息
        }
       
     }
     public function ajaxadd()        //发表愿望(异步刷新)
     {
        $model=D("notes"); 
        if($data=$model->create()) 
       {
            $d=date('Y-m-d H:i:s',time());
           $model->addtime=$d;
           $result=$model->add();
           if($result){
              //$data['name']=$model.name;
              $data['id']=$result;
              //$data['content']=$model.content;
              $data['addtime']=$d;
              $data['isok']=1;
              $this->ajaxReturn($data); 
            }
           else
            {
               $data['isok']=0;
               $data['msg']='许愿出错！';
                $this->ajaxReturn($data);
	    } 
           } 
           else //数据通不过验证
           {
               $data['isok']=0;
               $data['msg']=$model->getError();
            $this->ajaxReturn($data);//输出错误提示信息
        }
        
     }
     
     public function ajaxdel()   //删除愿望（异步删除）
    {       
       $id=I('get.id',0);
	
       $model=M('notes'); 	     
	$rs=$model->delete($id);
	if ($rs)
	   { 
            $data["isok"]=1;
          $this->ajaxReturn($data);  
	   }
	   else
	   { 
               $data["isok"]=0;
         $this->ajaxReturn($data);
	   }
          
    }
}